

*Assign the directory for the table + figure output
global tnf .\TablesAndFigures\

 
********************************************************************************	
* Table 3 - Summary statistics for the credit bureau/HMDA matched panel
********************************************************************************
* The code below writes output that can easily be pasted into a LaTex table.  
use Data_CBAllPersonYears.dta, clear // synthetic data created in _0_GenerateSyntheticData.do

***** Select the overall sample
*For Panel A (2010 snapshot):
local moreif & year==2010 
*For Panel B (Auto loan applicants 2005-2017): 
*local moreif & aua_accessratio_annual_w0 !=.

*Define subsamples (columns 1-5 of Table 3)
local ifcondition1 "if year!=. `moreif'" // overall sample (year is never missing)
local ifcondition2 "if matched==1 `moreif'" // subset matched to HMDA
local ifcondition3 "if race==1 `moreif'" // matched to HMDA and white
local ifcondition4 "if race==3 `moreif'" // matched to HMDA and Black
local ifcondition5 "if race==2 `moreif'" // matched to HMDA and Hispanic

local varlist aua_accessratio_annual_w0 l1_vantage_v3_score age l1_d_havedebt_mta l1_all_balance_open l1_all_pastdue2 l1_aua_balance_open hmda_app_income new_dti

local n: word count `varlist'
forvalues i = 1/`n' {
    local var : word `i' of `varlist'
	
	forvalues s=1/5 {
		qui summ `var' `ifcondition`s'', detail
		local mean`s' `r(mean)'
		local sd`s' `r(sd)'	
		}
	
	if `i'==1 {
		di "" _col(30) " & mean(g1)" _col(40) " & mean(g2)" _col(50) " & mean(g3)" _col(60) "  & mean(g4)" _col(70) "  & mean(g5)   \\"
		}
	di " `:var label `var''" _col(30) " & `:di %12.3g `mean1''" _col(40) " & `:di %12.3g `mean2''" _col(50) " & `:di %12.3g `mean3''" _col(60)  " & `:di %12.3g `mean4''" _col(70) " & `:di %12.3g `mean5'' \\"
	di " "    " & \begin{scriptsize}[`:di %12.3g `sd1'']\end{scriptsize}"    " & \begin{scriptsize}[`:di %12.3g `sd2'']\end{scriptsize}"      " & \begin{scriptsize}[`:di %12.3g `sd3'']\end{scriptsize}"      " & \begin{scriptsize}[`:di %12.3g `sd4'']\end{scriptsize}"     " & \begin{scriptsize}[`:di %12.3g `sd5'']\end{scriptsize} \\"
	}	

	

********************************************************************************	
* Table 4 - Effect of applicant race on auto credit approval
********************************************************************************
use Data_MainMatchedPanel, clear // synthetic data created in _0_GenerateSyntheticData.do

global y aua_accessratio_annual_w0
global democontrols sex_female age ln_hmda_app_income
global creditscore l1_vantage_v3_score
global creditcontrols ln_l1_all_balance_open new_dti ln_l1_all_pastdue2  	   
global zipcontrols ln_acs_pipc ln_popdensity acs_edu_bagrad acs_commute_usecar
global FE timetomort statefips#year 

estimates clear
local moreif 
*build up to full set of controls
eststo: reghdfe ${y} race_minority ${democontrols} ${zipcontrols} ///
        if ${y}!=. `moreif', absorb(${FE}) cluster(statefips#year)  	
eststo: reghdfe ${y} race_minority ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        if ${y}!=. `moreif', absorb(${FE}) cluster(statefips#year)   	
*hispanic vs black
eststo: reghdfe ${y} race_minority race_hispanic ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        if ${y}!=. `moreif', absorb(${FE}) cluster(statefips#year)  
*subprime 
eststo: reghdfe ${y} race_minority ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        if l1_subprime==1 `moreif', absorb(${FE}) cluster(statefips#year)    	
*prime
eststo: reghdfe ${y} race_minority ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        if l1_subprime==0 `moreif', absorb(${FE}) cluster(statefips#year)    		

la var race_hispanic "Minority X Hispanic"	
esttab _all using ${tnf}T_CreditAccess_Auto ,title("") ///
	replace tex frag ///
	order(race_minority race_hispanic ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols}) noomitted  b(%12.3f) se(%12.3f) transform(100*@ 100)   ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nomtitles   ///
	mgroups("Full Sample" "Subprime Borrowers" "Prime Borrowers", pattern(0 0 0 1 1 1)  span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 	
la var race_hispanic "Hispanic"		
	
	
	
********************************************************************************	
* Table 5 - Where does applicant race have the largest impact on auto credit approval?	
********************************************************************************
*note: continue using the same data and globals defined for Table 4.	

estimates clear
local ifcond 
*baseline
eststo: reghdfe ${y} race_minority ///
		${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)   	

*interact w racial bias
eststo: reghdfe ${y} race_minority race_minorityXbias ///
        ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)  
		
*interact w low banking competition
eststo: reghdfe ${y} race_minority race_minorityXlowcomp lowcomp ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)
				
*interact w low dealer competition
eststo: reghdfe ${y} race_minority race_minorityXlowdealercomp lowdealercomp ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)		
		
*interact w high non-bank financing share
eststo: reghdfe ${y} race_minority race_minorityXnonbank_hi nonbank_hi ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 			
		
*interact w rural
eststo: reghdfe ${y} race_minority race_minorityXrural rural ///
        ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)   
					
esttab _all using ${tnf}T_CreditAccessInts_Auto ,title("") ///
	replace tex frag ///
	drop(${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols}) noomitted b(%12.3f) se(%12.3f) transform(100*@ 100)   ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nonumbers 



********************************************************************************	
* Table 6 - Falsification test: Race and credit card lending 	
********************************************************************************
*note: continue using the same data and globals defined for Table 4.

estimates clear
*now set the y as the credit card limit increase:
	global y bcc_credit_inc  
*restrict sample to those who search for both auto credit and CC credit during the year:	
	local ifcond if aua_accessratio_annual_w0!=. & bcc_accessratio_annual_w0!=. 

*baseline
eststo: reghdfe ${y} race_minority ///
        ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)  
*interact w racial bias
eststo: reghdfe ${y} race_minority race_minorityXbias ///
        ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)  
*interact w low banking competition
eststo: reghdfe ${y} race_minority race_minorityXlowcomp lowcomp ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)
*interact w low dealer competition
eststo: reghdfe ${y} race_minority race_minorityXlowdealercomp lowdealercomp ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE})	cluster(statefips#year)	
*interact w high non-bank financing share
eststo: reghdfe ${y} race_minority race_minorityXnonbank_hi nonbank_hi ///
         ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)					
*interact w rural
eststo: reghdfe ${y} race_minority race_minorityXrural rural ///
        ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)   
					
esttab _all using ${tnf}T_CreditAccessInts_CCLimit ,title("") ///
	replace tex frag  ///
	drop(${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols}) noomitted b(%12.2f) se(%12.2f)   ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nonumbers 



********************************************************************************
* Produce Figures 1 and 2
********************************************************************************

*** Step 1: Using the actual credit bureau/HMDA matched panel, we ran the following 
***         code to estimate minorities' reduction in auto loan approval in each state 		
/*
*Define minority X state interactions
forvalues v=1/56 {
	gen s`v' = 0
	replace s`v' = 1 if statefips==`v'
	summ s`v'
	if `r(mean)' ==0 {
		drop s`v'
		}
	capture gen minorityXs`v' = s`v' * race_minority
	capture drop s`v'
	}

*Run credit approval regression, with minority X state interactions  
reghdfe aua_accessratio_annual_w0 minorityXs* ${democontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
		, absorb($FE) cluster(statefips#year)	
regsave using Data_StateEstimates, replace
*/		
		
*** Step 2: We merged on the number of minority applications in each state, the google 
***         racial bias measure, state names + abbreviations, etc. The actual resultant
***         data set is included here as Data_StateEstimates.dta		

*** Step 3: Here is the code to produce Figures 1 and 2:		

*****************
* Figure 1 - Scatter plot of estimated minority coefficient on racial bias index
*****************
use Data_StateEstimates, clear // data set of state estimates from the actual data

pwcorr _b_race_minority racialbias_googleupdated [aweight=Nminorities], sig
reg    _b_race_minority racialbias_googleupdated [aweight=Nminorities] , robust

twoway (lfit _b_race_minority racialbias_googleupdated [aweight=Nminorities], lpattern(dash) lwidth(thin) legend(off)) ///
	   ( scatter _b_race_minority racialbias_googleupdated [w=Nminorities], msymbol(circle_hollow) mcolor(gs14) )  ///
       ( scatter _b_race_minority racialbias_googleupdated , mlabel(stateabbrev) mlabsize(vsmall) mlabposition(0) msymbol(i) mlabcolor(black)  ///
	   ytitle("Minorities' Estimated Reduction" "in Auto Loan Approval") xlabel(25(25)100) xtitle(Google Search Volume for Racial Slurs) ) ///
	   , name(StateEstimatesScatter, replace)	 
	   
graph export ${tnf}F_StateEstimatesScatter_FE_weighted.pdf , replace name(StateEstimatesScatter)
	   
*****************
* Figure 2 - Map with estimated minority coefficient's statistical significance
*****************
use Data_StateEstimates, clear // data set of state estimates from the actual data

gen tstat = abs(_b_race_minority / _se_race_minority)
gen statsignif = 1 if _b_race_minority >0 & insuffdata==0
replace statsignif = 0 if _b_race_minority <=0 & tstat<1.65 & insuffdata==0
replace statsignif = -1 if _b_race_minority <=0 & tstat>=1.65 & insuffdata==0

*merge on _ID to make map using spmap
merge 1:1 statefips using statemap50_fipslink.dta, update
drop if _merge==2
sort _ID

spmap statsignif using statemap50_coord.dta, name(F2, replace) id(_ID) ///
      clmethod(unique) fcolor(gs2 gs8 gs14) ndfcolor(white) ndocolor(gs14) osize(medthick) ocolor(black) ///
	  legend(position(6) ring(1) title("Estimated Minority Coefficient") label(1 "Insufficient Data") label(2 "Negative, Statistically Significant") label(3 "Negative, Statistically Insignificant") label(4 "Positive Point Estimate") ) 
	  
graph export ${tnf}F_StateEstimatesMap_tstat_FE.pdf , replace name(F2)


		
		
		
		
		
		
		
	